import com.vesoft.nebula.client.graph.NebulaPoolConfig;
import com.vesoft.nebula.client.graph.data.HostAddress;
import com.vesoft.nebula.client.graph.data.ResultSet;
import com.vesoft.nebula.client.graph.net.NebulaPool;
import com.vesoft.nebula.client.graph.net.Session;
import org.junit.Test;

import java.util.Arrays;
import java.util.List;

/**
 * @author dinghao
 * @create 2021-09-18 17:10
 * @message
 */
public class NebulaTest {
    @Test
    public void test1() throws Exception {
        NebulaPoolConfig nebulaPoolConfig = new NebulaPoolConfig();
        nebulaPoolConfig.setMaxConnSize(10);
        List<HostAddress> addresses = Arrays.asList(new HostAddress("127.0.0.1", 9669),
                new HostAddress("127.0.0.1", 9670));
        NebulaPool pool = new NebulaPool();
        pool.init(addresses, nebulaPoolConfig);
        Session session = pool.getSession("root", "nebula", false);
        //创建图空间
        session.execute("CREATE SPACE basketballplayer(partition_num=15, replica_factor=1, vid_type=fixed_string(30))");
        //选择图空间
        session.execute("USE basketballplayer");
        //创建点和边
        session.execute("CREATE TAG player(name string, age int)");
        session.execute("CREATE TAG team(name string)");
        session.execute("CREATE EDGE follow(degree int)");
        session.execute(" CREATE EDGE serve(start_year int, end_year int)");
        //插入点和边
        session.execute("INSERT VERTEX player(name, age) VALUES \"player100\":(\"Tim Duncan\", 42)");
        session.execute("INSERT VERTEX player(name, age) VALUES \"player101\":(\"Tony Parker\", 36)");
        session.execute("INSERT VERTEX player(name, age) VALUES \"player102\":(\"LaMarcus Aldridge\", 33)");
        session.execute("INSERT VERTEX team(name) VALUES \"team200\":(\"Warriors\"), \"team201\":(\"Nuggets\")");
        session.execute("INSERT EDGE follow(degree) VALUES \"player100\" -> \"player101\":(95)");
        session.execute("INSERT EDGE follow(degree) VALUES \"player100\" -> \"player102\":(90)");
        session.execute("INSERT EDGE follow(degree) VALUES \"player102\" -> \"player101\":(75)");
        session.execute("INSERT EDGE serve(start_year, end_year) VALUES \"player100\" -> \"team200\":(1997, 2016), \"player101\" -> \"team201\":(1999,  2018)");
        //查询
        session.execute("FETCH PROP ON player \"player100\"");
        //修改点边
        session.execute("UPDATE VERTEX \"player100\" SET player.name = \"Tim\"");
        session.execute("UPDATE EDGE \"player100\" -> \"player101\" OF follow SET degree = 96");
        //删除点边
        session.execute("DELETE VERTEX \"team1\", \"team2\"");
        session.execute("DELETE EDGE follow \"team1\" -> \"team2\"");
        //创建索引
        session.execute("CREATE TAG INDEX player_index_0 on player(name(20))");
        session.release();
        pool.close();
    }

    //TODO 6、图数据库的读写接口
    @Test
    public void test2() throws Exception {
        NebulaPool pool = new NebulaPool();
        Session session = null;
        try {
            NebulaPoolConfig nebulaPoolConfig = new NebulaPoolConfig();
            nebulaPoolConfig.setMaxConnSize(100);
            List<HostAddress> addresses = Arrays.asList(new HostAddress("192.168.1.153", 9669));
            pool.init(addresses, nebulaPoolConfig);
            session = pool.getSession("root", "nebula", false);

            //create space
            String space = "test";
            String createSpace = "CREATE SPACE IF NOT EXISTS " + space + " (partition_num=15, replica_factor=1, vid_type=fixed_string(30)); ";
            ResultSet resp1 = session.execute(createSpace);


            //create schema
            String createSchema = "USE " + space + "; CREATE TAG IF NOT EXISTS person(name string, age int);"
                    + "CREATE EDGE IF NOT EXISTS like(likeness double)";
            ResultSet resp2 = session.execute(createSchema);

            //insert vertex
            String insertVertexes = "INSERT VERTEX person(name, age) VALUES " + "'Bob':('Bob', 10), "
                    + "'Lily':('Lily', 9), " + "'Tom':('Tom', 10), " + "'Jerry':('Jerry', 13), "
                    + "'John':('John', 11);";
            ResultSet resp3 = session.execute(insertVertexes);

            // inert edge
            String insertEdges = "INSERT EDGE like(likeness) VALUES " + "'Bob'->'Lily':(80.0), "
                    + "'Bob'->'Tom':(70.0), " + "'Lily'->'Jerry':(84.0), " + "'Tom'->'Jerry':(68.3), "
                    + "'Bob'->'John':(97.2);";
            ResultSet resp4 = session.execute(insertEdges);

            // query
            String query = "GO FROM \"Bob\" OVER like " + "YIELD $$.person.name, $$.person.age, like.likeness";
            ResultSet resp5 = session.execute(query);
            System.out.println(resp5.isSucceeded());
            System.out.println(resp5);
        }finally {
            if (session != null) {
                session.release();
            }
            pool.close();
        }
    }
}
